/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.beynet.docs.ui;

import java.security.PrivilegedAction;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import org.apache.log4j.Logger;
import org.beynet.docs.format.Collection;

/**
 *
 * @author beynet
 */
public class CollectionTreeNode extends DefaultMutableTreeNode implements  PrivilegedAction<Object>{
    public CollectionTreeNode(Collection obj,DefaultTreeModel model) {
        super(obj);
        childsRetrieved = false ;
        add(new DefaultMutableTreeNode());
        this.model = model;
    }
    
    
    @Override
    public Object run() {
        if (childsRetrieved == false) {
            removeAllChildren();
            try {
                for (Collection col : getUserObject().getChilds()) {
                    add(new CollectionTreeNode(col,model));
                }
            } catch (Exception e) {
                logger.error("Error retrieving child list for " + getUserObject().getName(), e);
            }
            childsRetrieved = true ;
            model.reload(this);
        }
        return (null);
    }

    @Override
    public Collection getUserObject() {
        return (Collection)super.getUserObject();
    }

    @Override
    public String toString() {
        return getUserObject().getName();
    }
    
    
    private boolean childsRetrieved = false ;
    private DefaultTreeModel model ;
    private static final Logger logger = Logger.getLogger(CollectionTreeNode.class);
    
}
